package service

import (
	"ruoyi-go/app/admin/model/system"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/pkg/mysql"
	"ruoyi-go/utils"
	"ruoyi-go/utils/R"
)

var SysPostsService = &sysPostsService{}

type sysPostsService struct{}

func (service sysPostsService) SelectUserPostGroup(userName string) string {
	var posts []system.SysPost
	var result = ""
	mysql.MysqlDb().Raw("select p.post_id, p.post_name, p.post_code "+
		"from sys_post p "+
		"left join sys_user_post up on up.post_id = p.post_id "+
		"left join sys_user u on u.user_id = up.user_id "+
		"where u.user_name = ?", userName).Scan(&posts)
	for i := range posts {
		sysPost := posts[i]
		if i == 0 {
			result = sysPost.PostName
		} else {
			result += "," + sysPost.PostName
		}
	}

	return result
}

// 分页查询
func (service sysPostsService) SelectSysPostList(params tools.SearchTableDataParam, isPage bool) tools.TableDataInfo {

	var pageNum = params.PageNum
	var pageSize = params.PageSize
	sysPost := params.Other.(system.SysPost)

	offset := (pageNum - 1) * pageSize
	var total int64
	var rows []system.SysPost

	var db = mysql.MysqlDb().Model(&rows)

	var postCode = sysPost.PostCode
	if postCode != "" {
		db.Where("post_code like ?", "%"+postCode+"%")
	}
	var status = sysPost.Status
	if status != "" {
		db.Where("status = ?", status)
	}
	var postName = sysPost.PostName
	if postName != "" {
		db.Where("post_name like ?", "%"+postName+"%")
	}

	db.Order("post_sort")

	if err := db.Count(&total).Error; err != nil {
		return tools.Fail()
	}
	if isPage {
		if err := db.Limit(pageSize).Offset(offset).Find(&rows).Error; err != nil {
			return tools.Fail()
		}
	} else {
		if err := db.Find(&rows).Error; err != nil {
			return tools.Fail()
		}
	}

	if rows == nil {
		return tools.Fail()
	} else {
		return tools.Success(rows, total)
	}
}

func (service sysPostsService) SelectPostListByUserId(userId int) []int {
	var result []int
	mysql.MysqlDb().Raw("select p.post_id "+
		"from sys_post p "+
		"left join sys_user_post up on up.post_id = p.post_id "+
		"left join sys_user u on u.user_id = up.user_id "+
		"where u.user_id = ? ", userId).Find(&result)
	return result
}

/*
*添加 岗位和用户关联
 */
func (service sysPostsService) AddPostByUser(user system.SysUserParm) {
	postIds := user.PostIds
	userId := user.UserId
	service.AddPostListByUser(postIds, userId)
}

func (service sysPostsService) AddPostListByUser(postIds []int, userId int) {
	var posts []system.SysUserPost
	for i := 0; i < len(postIds); i++ {
		var post = system.SysUserPost{
			PostId: postIds[i],
			UserId: userId,
		}
		posts = append(posts, post)
	}
	err := mysql.MysqlDb().CreateInBatches(posts, len(posts)).Error
	if err != nil {
		panic(R.ReturnFailMsg("添加 部门管理用户失败"))
	}
}

// 删除关联用户
func (service sysPostsService) DeletePostByUser(userIds []int) {
	err := mysql.MysqlDb().Exec("delete from sys_user_post where user_id in (?) ", userIds).Error
	if err != nil {
		panic(R.ReturnFailMsg("删除部门关联用户失败"))
	}
}

func (service sysPostsService) FindPostInfoById(postId string) system.SysPost {
	var post system.SysPost
	err := mysql.MysqlDb().Where("post_id = ?", postId).First(&post).Error
	if err != nil {
		panic(err.Error())
	}
	return post
}

func (service sysPostsService) SavePost(post system.SysPost) {
	err := mysql.MysqlDb().Model(&system.SysPost{}).Create(&post).Error
	if err != nil {
		panic(err.Error())
	}
}

func (service sysPostsService) EditPost(sysPost system.SysPost) {
	err := mysql.MysqlDb().Updates(&sysPost).Error
	if err != nil {
		panic(err.Error())
	}
}

func (service sysPostsService) DeletePost(postIds string) {
	err := mysql.MysqlDb().Where("post_id in (?)", utils.Split(postIds)).Delete(&system.SysPost{}).Error
	if err != nil {
		panic(err.Error())
	}
}
